package com.hpin.fansea.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.my.MyOrderActivity;
import com.hpin.fansea.my.PayRecordActivity;
import com.hpin.fansea.utils.Constant;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 *  Aaron
 *  2015-10-30
 */
public class WXPayEntryActivity extends BaseActivity implements
		IWXAPIEventHandler {

	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay_result);

//		LogUtil.e("tag", "====WXPayEntryActivity====");

		api = WXAPIFactory.createWXAPI(this, WXKeys.APP_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	public void widgetClick(View v) {

	}


	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	//微信支付完成的回调
	@Override
	public void onResp(BaseResp resp) {

//		LogUtil.e("tag", "====onResp====" + resp.getType() + "\r\n" + resp.errStr
//		 + "\r\n" + resp.checkArgs() + "\r\n" + resp.openId);

		if(resp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){


			switch (resp.errCode) {

				case -2:

					//用户取消支付
					finish();

					break;

				case 0:

					//支付完成
					if (Constant.PAY_GOODS.equals(mApplication.wxPayType)) {
						//商品支付 跳 我的订单页面
						Intent nIntent = new Intent(WXPayEntryActivity.this, MyOrderActivity.class);
						startActivity(nIntent);

					} else if (Constant.PAY_PROPERTY.equals(mApplication.wxPayType)) {
						//物业支付 跳 缴费页面
						Intent mIntent = new Intent(WXPayEntryActivity.this, PayRecordActivity.class);
						startActivity(mIntent);
					}

					break;

			}

//			LogUtil.d("tag", "onPayFinish,errCode=" + resp.errCode);
//			AlertDialog.Builder builder=new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip).show();

		}

	}
}